package Messages;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.Statement;

import ocsf.server.ConnectionToClient;

import server.SQLConnection;
import Control.UserDirector;
import Control.UserLibrarian;
import Control.UserSubscriber;
/**
 * this class implements the login authorization
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class MessageLogin {
	
	private ConnectionToClient client ;
	private String[] str; 
	
/**
 * Contractor of this class 
 * @param str
 * @param client
 */
	public MessageLogin(String[] str, ConnectionToClient client){
		
		this.client= client ;
		this.str= str; 
	}
	/**
	 * Authorize the login 
	 */
	public void Autorization(){
	  try 
      {  
          Statement stmt;
          SQLConnection conn= new SQLConnection();
          java.sql.PreparedStatement ps = conn.getConnection().prepareStatement("SELECT u.`ID`, u.`Password` ,u.`Role`,u.`Connected`,u.`status` FROM DB1.subscriber u WHERE ID='"+str[1]+"' AND Password='"+str[2]+"';");
          ResultSet rs = ps.executeQuery();
          if (!rs.next()){
        	  ps.close();
	          rs.close();
	          SQLConnection conn2= new SQLConnection();
	          java.sql.PreparedStatement ps2 = conn2.getConnection().prepareStatement("SELECT u.`WorkerId`, u.`Password` ,u.`Role`,u.`Connected` FROM DB1.workers u WHERE WorkerId='"+str[1]+"' AND Password='"+str[2]+"';");
	          ResultSet rs2 = ps2.executeQuery();
	          if (!rs2.next()){						
				client.sendToClient("NotInTheSystem");
				}
	          
	          else{
	        	  if(rs2.getString(3).equals("1"))
					{
	        		  	if(rs2.getString(4).equals("1")){
	                	  client.sendToClient("MultipleConnection");
	                	  }
	        		  	else{
	        		  		java.sql.PreparedStatement psl = conn2.getConnection().prepareStatement("Update DB1.workers SET Connected=? WHERE WorkerId=?");	
		        		  	psl.setInt(1, 1);
		        		  	psl.setString(2, str[1]);
		        		  	psl.executeUpdate();

						UserLibrarian userLibrarian = new UserLibrarian(rs2.getString(1),rs2.getString(2),rs2.getString(3),true);
						System.out.println("userLibrarian is logged in: " + userLibrarian.getUserName() +" "+ userLibrarian.getPassword());
						try 
						{
							System.out.println("try client.sendToClient(UserLibrarian);");
							client.sendToClient("try client.sendToClient(UserLibrarian);");
							client.sendToClient(userLibrarian);
						} catch (IOException e)	{
							e.printStackTrace();
						}
	        		  	}
					}
					if(rs2.getString(3).equals("0"))
					{
						if(rs2.getString(4).equals("1")){
		                	  client.sendToClient("MultipleConnection");
		                	  }
		        		else{
		        			java.sql.PreparedStatement psl = conn2.getConnection().prepareStatement("Update DB1.workers SET Connected=? WHERE WorkerId=?");	
		        		  	psl.setInt(1, 1);
		        		  	psl.setString(2, str[1]);
		        		  	psl.executeUpdate();
						UserDirector UserDirector = new UserDirector(rs2.getString(1),rs2.getString(2),rs2.getString(3),true);
						System.out.println("UserDirector is logged in: " + UserDirector.getUserName()+" "+ UserDirector.getPassword() );
						try 
						{
							System.out.println("try client.sendToClient(UserDirector);");
							client.sendToClient(UserDirector);
						} catch (IOException e)	{
							e.printStackTrace();
						}
					}
					}
					 ps2.close();
			         	
					rs2.close();
					} 
          }
	          else{
	         
					if(rs.getString(3).equals("2"))   // if user have subscriber permissions
					{	
						if(rs.getString(4).equals("1")){
		                	  client.sendToClient("MultipleConnection");
		                	  }
						
						else if (rs.getInt(5)==1)
						{
							client.sendToClient("Lock");
							
						}
						
						else if (rs.getInt(5)==2)
						{
							client.sendToClient("frezzed");
							
						}
		                		  
		        		else{
		        			java.sql.PreparedStatement psl = conn.getConnection().prepareStatement("Update DB1.subscriber SET Connected=? WHERE ID=?");	
		        		  	psl.setInt(1, 1);
		        		  	psl.setString(2, str[1]);
		        		  	psl.executeUpdate();
						UserSubscriber userSubscriber = new UserSubscriber(rs.getString(1),rs.getString(2),rs.getString(3), true );
						System.out.println("userSubscriber is logged in: " + userSubscriber.getUserName() +" "+userSubscriber.getPassword() );
						
						
						try 
						{
							System.out.println("try client.sendToClient(userSubscriber);");
							client.sendToClient(userSubscriber);
						} catch (IOException e)	{
							e.printStackTrace();
						}
						
						
						}
					}
	          }
	        	  
	        	  
          }
	       catch (Exception e)
	       {
	    	   e.printStackTrace();
	       
	       }
	 


	
	
	
	}
	
	
	
	
	}
